--
-- Author: shunguo.chen
-- Date: 2018-07-27 15:02:36
--
-- TheatreBossRender 歌剧院boss进度render
--

local string_format = string.format

local ClassRef = app.mvc.createRenderClass(...)

local COLOR_MY = cc.c3b( 64,233,249 )
local COLOR_ENEMY = cc.c3b( 253,103,63 )
local PERCENT_STR = "%s%%"

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:refresh()
end

-- data = percent
function ClassRef:refresh()
    local hasData = self._data ~= nil
    self._ui.LoadingBar:setVisible(hasData)
    self._ui.num:setVisible(hasData)
    if self._data then
        self._ui.num:setString(string_format(PERCENT_STR, self._data))
        self._ui.LoadingBar:setPercent(self._data)
    end
end

function ClassRef:setNumVisible(b)
    self._ui.num:setVisible(b)
end

function ClassRef:setIsWin(b)
    self._ui.icon_1:setVisible(b)
    self._ui.bg:setVisible(not b)
end

function ClassRef:setIsEnemy(b)
    self._ui.LoadingBar:setColor(b and COLOR_ENEMY or COLOR_MY)
    self._ui.num:setColor(b and COLOR_ENEMY or COLOR_MY)
end

-- 播放百分比减少到0的动画
function ClassRef:playReduceAction(leftPer, cb)
    local percent = leftPer + 1
    local numText = self._ui.num
    local bar = self._ui.LoadingBar
    self:schedule(function()
        percent = percent - 1
        if percent < 0 then
            executeFunc(cb)
            self:stopAllActions()
            self:setData()
            self:setIsWin(false)
        else
            numText:setString(string_format(PERCENT_STR, percent))
            bar:setPercent(percent)
        end
    end, 0)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
